//
//  TGUploadVideoTool.m
//  TongGuanLease
//
//  Created by 张奇 on 2022/8/31.
//

#import "TGUploadVideoTool.h"
#import "TGUrlInfoModel.h"
#import "ZLPhotoBrowser.h"

@interface TGUploadVideoTool ()
@property (nonatomic, strong) NSMutableArray *listOfDownLoadVideoArr;
@property (nonatomic, assign) NSInteger allCount;
@property (nonatomic, copy) UploadVideoComplemention complementionBlock;
@property (nonatomic, strong) NSMutableArray <TGUrlInfoModel *>*resultUrlInfoArray;
@property (nonatomic, copy) NSString *filePath;
@end

@implementation TGUploadVideoTool
//-------------------------------------------------视频-----------------------------------------------

- (void)uploadVideoWithFilePath:(NSString *)filePath videoArray:(NSArray <PHAsset*>*)videoArray complemention:(UploadVideoComplemention)complemention {
    
    self.listOfDownLoadVideoArr = [NSMutableArray arrayWithArray:videoArray];
    self.allCount = videoArray.count;
    self.complementionBlock = complemention;
    self.resultUrlInfoArray = [NSMutableArray array];
    self.filePath = filePath;
    [self begainUploadVideo];
}

- (void)begainUploadVideo {
    
    if (self.listOfDownLoadVideoArr.count > 0) {
        id firstVideo = self.listOfDownLoadVideoArr.firstObject;
        
        //            WEAK;
        [ZLPhotoManager exportVideoForAsset:firstVideo type:ZLExportVideoTypeMp4 complete:^(NSString * _Nonnull exportFilePath, NSError * _Nonnull error) {
            //                STRONG;
            NSLog(@"导出视频file %@ ,error = %@",exportFilePath,error);
            if (EmptyCheck(exportFilePath)) {
                [TGProgressHUD showMBHUDErrorTip:@"该视频无法上传，请选择其他"];
            }
            NSMutableDictionary *videoInfo = [NSMutableDictionary dictionaryWithDictionary:[self getVideoInfoWithSourcePath:exportFilePath]];
            PHAsset *asset = firstVideo;
            [videoInfo setObject:[NSString stringWithFormat:@"%.2f", (asset.pixelWidth*1.0)/(asset.pixelHeight*1.0)] forKey:@"proportion"];
            NSLog(@"====%@=", videoInfo);
            if ([videoInfo[@"size"] floatValue] > 100) {
                [TGGCD executeAfterTime:1 task:^{
                    [TGProgressHUD showHUDTip:@"该视频大于100M无法上传，请选择其他视频"];
                }];
                self.complementionBlock(@"", [NSError new], YES, nil);
                return;
            }
            
            [TGNetworkManager uploadVideo:exportFilePath filePath:self.filePath success:^(TGResponseItem *responseItem) {
                if (responseItem.success) {
                    
                    TGUrlInfoModel *urlInfoModel = [TGUrlInfoModel modelWithJSON:responseItem.data];
                    urlInfoModel.size = urlInfoModel.size/1024/1024.0;
                    [self.resultUrlInfoArray addObject:urlInfoModel];
                    [self.listOfDownLoadVideoArr removeObjectAtIndex:0];
                    NSString *pStr = [NSString stringWithFormat:@"%zd/%zd", (long)(self.allCount-self.listOfDownLoadVideoArr.count), (long)self.allCount];
                    self.complementionBlock(pStr, nil, NO, nil);
                    [self begainUploadVideo];
                    
                } else {
                    
                    if (self.allCount > 1) {
                        NSString *pStr = [NSString stringWithFormat:@"%zd(%.0f%%)/%zd", (long)(self.allCount-self.listOfDownLoadVideoArr.count), responseItem.uploadProgress*100, (long)self.allCount];
                        self.complementionBlock(pStr, nil, NO, nil);
                    } else {
                        NSString *pStr = [NSString stringWithFormat:@"%.0f%%", responseItem.uploadProgress*100];
                            self.complementionBlock(pStr, nil, NO, nil);
                    }
                }
            } failure:^(NSError *error) {
                self.complementionBlock(@"", error, YES, nil);
            }];
            
        }];
        
    } else {
        self.complementionBlock(@"完成", nil, YES, self.resultUrlInfoArray);
    }
}

- (NSDictionary *)getVideoInfoWithSourcePath:(NSString *)path{
    AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];
    CMTime   time = [asset duration];
    int seconds = ceil(time.value/time.timescale);

    NSInteger fileSize = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil].fileSize;
    
    return @{@"size" : @(fileSize/1024/1024.0),
             @"duration" : @(seconds)};
}

@end
